package com.hdjk.view.activity.personalcenter;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.RelativeLayout;

import com.hdjk.view.R;
import com.hdjk.view.adapter.BankCardListAdapter;
import com.hdjk.view.callback.CallBackListener;
import com.hdjk.view.dialog.TextButtonDialog;
import com.hdjk.view.entity.BankEntity;
import com.hdjk.view.entity.PayMethodEntity;
import com.hdjk.view.network.BankCardNetWork;
import com.hdjk.view.network.Network;
import com.hdjk.view.network.TransactionNetWork;
import com.hdjk.view.util.SharedPreferencesUtil;
import com.hdjk.view.util.StringAppUtil;
import com.hdjk.view.util.ToastUtils;
import com.hdjk.view.util.UserTokenUtil;
import com.lst.projectlib.base.BaseActivity;
import com.lst.projectlib.component.ListView.ListViewForScrollView;
import com.lst.projectlib.entity.Msg;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 银行卡列表
 * Created by sxy on 2017/11/10.
 */

public class BankCardListActivity extends BaseActivity implements View.OnClickListener {
    private List<BankEntity> mList;
    private BankCardListAdapter mAdapter;
    private List<BankEntity> mBankList;
    private TextButtonDialog mDelDialog;
    // Content View Elements
    private com.lst.projectlib.component.CustomView.TopTitleBar ttb_title_bar;
    private ListViewForScrollView lv_data_list;
    private RelativeLayout rel_add;
    // End Of Content View Elements

    private void bindViews() {
        ttb_title_bar = (com.lst.projectlib.component.CustomView.TopTitleBar) findViewById(R.id.ttb_title_bar);
        lv_data_list = (ListViewForScrollView) findViewById(R.id.lv_data_list);
        rel_add= (RelativeLayout) findViewById(R.id.rel_add);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_bank_card_list);
        bindViews();
        ttb_title_bar.findViewById(R.id.tvNavBack).setOnClickListener(this);
        rel_add.setOnClickListener(this);
    }

    @Override
    protected void initData() {
        mList = new ArrayList<>();
        mAdapter = new BankCardListAdapter(this);
        mAdapter.setDataList(mList);
        mAdapter.setOnDelClick(new BankCardListAdapter.OnDelClickListener() {
            @Override
            public void OnDelClick(final int position) {
                mDelDialog = new TextButtonDialog(BankCardListActivity.this, R.style.progress_dialog, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        switch (v.getId()) {
                            case R.id.tv_cancel:
                                mDelDialog.dismiss();
                                break;
                            case R.id.tv_sure:
                                mDelDialog.dismiss();
                                JSONObject obj = new JSONObject();
                                try {
                                    mDialog.show();
                                    obj.put("accountNumber", UserTokenUtil.getUserName(BankCardListActivity.this));
                                    obj.put("investorPayId", mList.get(position).getInvestorPayId());
                                    BankCardNetWork.DelBankCard(UserTokenUtil.getToken(BankCardListActivity.this), obj.toString(), new CallBackListener() {
                                        @Override
                                        public void onCallBackCompleted(Msg msg) {
                                            mDialog.cancel();
                                            if (msg != null && msg.getSuccess()) {
                                                ToastUtils.showShortToast(BankCardListActivity.this, "删除成功");
                                                getBankList();
                                            } else {
                                                if (msg != null && !StringAppUtil.isEmptyOrNull(msg.getMsg())) {
                                                    ToastUtils.showShortToast(BankCardListActivity.this, msg.getMsg());
                                                } else {
                                                    ToastUtils.showShortToast(BankCardListActivity.this, "删除失败");
                                                }
                                            }
                                        }
                                        @Override
                                        public void onCallBackError(Msg msg) {
                                            mDialog.cancel();
                                        }
                                    });
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                                break;
                        }
                    }
                });
                mDelDialog.setDialogContent("您确定要删除该银行卡吗？", 15);
                mDelDialog.show();
            }
        });
        lv_data_list.setAdapter(mAdapter);
        lv_data_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            }
        });
        getBankList();
    }

    private void getBankList() {
        mBankList = SharedPreferencesUtil.getBankList(this);
        if (mBankList == null || mBankList.size() == 0) {
            Network.getBankList(UserTokenUtil.getToken(BankCardListActivity.this), new CallBackListener() {
                @Override
                public void onCallBackCompleted(Msg msg) {
                    if (msg != null) {
                        if (msg.getSuccess()) {
                            if (!TextUtils.isEmpty(msg.getResult()))
                                SharedPreferencesUtil.saveBankList(BankCardListActivity.this, msg.getResult());
                            mBankList = (List<BankEntity>) msg.getData();
                            getPayMethod();
                        }
                    }
                }

                @Override
                public void onCallBackError(Msg msg) {

                }
            });
        } else {
            getPayMethod();
        }
    }

    private void getPayMethod() {
        mDialog.show();
        JSONObject object = new JSONObject();
        try {
            object.put("accountNumber", UserTokenUtil.getUserName(BankCardListActivity.this));
            TransactionNetWork.getPayMethod(UserTokenUtil.getToken(BankCardListActivity.this), object.toString(), new CallBackListener() {
                @Override
                public void onCallBackCompleted(Msg msg) {
                    mDialog.cancel();
                    if (msg != null && msg.getSuccess() && msg.getData() != null && msg.getData() instanceof PayMethodEntity) {
                        PayMethodEntity payMethod = (PayMethodEntity) msg.getData();
                        if (payMethod.getInvestorBankDetails() != null && payMethod.getInvestorBankDetails().getData() != null) {
                            mList = payMethod.getInvestorBankDetails().getData();
                            for (int i = 0; i < mList.size(); i++) {
                                BankEntity bankCard = mList.get(i);
                                for (int j = 0; j < mBankList.size(); j++) {
                                    BankEntity bank = mBankList.get(j);
                                    if (bankCard.getBankCode().equals(bank.getBankCode())) {
                                        bankCard.setDailyTradingLimit(bank.getDailyTradingLimit());
                                        bankCard.setMonthlyTransactionLimit(bank.getMonthlyTransactionLimit());
                                        bankCard.setSingleTransactionLimit(bank.getSingleTransactionLimit());
                                        bankCard.setName(bank.getName());
                                        break;
                                    }
                                }
                            }
                            if (mAdapter == null) {
                                mAdapter = new BankCardListAdapter(BankCardListActivity.this);
                                mAdapter.setDataList(mList);
                                lv_data_list.setAdapter(mAdapter);
                            } else {
                                mAdapter.setDataList(mList);
                                mAdapter.notifyDataSetChanged();
                            }
                        }

                    }
                }

                @Override
                public void onCallBackError(Msg msg) {
                    mDialog.cancel();
                }
            });
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tvNavBack:
                back();
                break;
            case R.id.rel_add:
                Intent intent=new Intent(this,AddBankCardActivity.class);
                startActivityForResult(intent,21);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==21&&resultCode==741){
            getBankList();
        }
    }
}
